/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package colloque.helpers;

import colloque.HibernateUtil;
import colloque.MotCle;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Thomas
 */
public class MotCleHelper {

    Session session = null;

    /**
     *
     */
    public MotCleHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    /**
     * Retourne tous les mots clés
     * @return List<MotCle>
     */
    public List<MotCle> getallmc() {
        List<MotCle> Tabmc = new ArrayList<MotCle>();

        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from MotCle as MC ");
            Tabmc = q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Tabmc;

    }

    /**
     * Trouve un mot clé par son label
     * 
     * @param nommc
     * @return
     * @throws Exception
     */
    public MotCle getMotClebyName(String nommc) throws Exception {

        MotCle mc = new MotCle();

        Transaction tx = session.beginTransaction();
        Query q = session.createQuery("from MotCle as M where M.motclelabel ='" + nommc);
        mc = (MotCle) q.uniqueResult();
        if (mc == null) {
            throw new Exception("Le mot clé est introuvable");
        }
        return mc;

    }

    /**
     * Trouve un mot clé par son id
     * 
     * @param id
     * @return MotCle
     */
    public MotCle getMotClebyId(int id) {

        MotCle mc = new MotCle();
        try {

            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from MotCle as MC where MC.motcleid = " + id);
            mc = (MotCle) q.uniqueResult();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mc;

    }

    /**
     *
     * @throws Throwable
     */
    @Override
    protected void finalize() throws Throwable {

        try {

            super.finalize();
            // session.close();
            //more code can be written here as per need of application

        } catch (Exception e) {
        }






    }
}
